home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / Kubuntu 8.10 / kubuntu-8.10-desktop-i386.iso / casper / filesystem.squashfs / usr / share / hplip / info.py < prev    next >
Text File  |  2008-10-13  |  8KB  |  251 lines

  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. #
  4. # (c) Copyright 2003-2008 Hewlett-Packard Development Company, L.P.
  5. #
  6. # This program is free software; you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation; either version 2 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with this program; if not, write to the Free Software
  18. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
  19. #
  20. # Author: Don Welch
  21. #
  22.  
  23. __version__ = '4.1'
  24. __title__ = 'Device Information Utility'
  25. __doc__ = "Query a printer for both static model information and dynamic status."
  26.  
  27. # Std Lib
  28. import sys
  29. import getopt
  30. import time
  31. import os
  32.  
  33. # Local
  34. from base.g import *
  35. from base import device, status, utils
  36. from prnt import cups
  37.  
  38. USAGE = [(__doc__, "", "name", True),
  39.          ("Usage: hp-info [PRINTER|DEVICE-URI] [OPTIONS]", "", "summary", True),
  40.          utils.USAGE_ARGS,
  41.          utils.USAGE_DEVICE,
  42.          utils.USAGE_PRINTER,
  43.          utils.USAGE_SPACE,
  44.          utils.USAGE_OPTIONS,
  45.          ("Device ID mode:", "-i or --id (prints device ID only and exits)", "option", False),
  46.          utils.USAGE_BUS1, utils.USAGE_BUS2,
  47.          utils.USAGE_LOGGING1, utils.USAGE_LOGGING2, utils.USAGE_LOGGING3,
  48.          utils.USAGE_HELP,
  49.          utils.USAGE_SPACE,
  50.          utils.USAGE_NOTES,
  51.          utils.USAGE_STD_NOTES1, utils.USAGE_STD_NOTES2, 
  52.          utils.USAGE_SEEALSO,
  53.          ("hp-toolbox", "", "seealso", False),
  54.  
  55.          ]
  56.  
  57. def usage(typ='text'):
  58.     if typ == 'text':
  59.         utils.log_title(__title__, __version__)
  60.  
  61.     utils.format_text(USAGE, typ, __title__, 'hp-info', __version__)
  62.     sys.exit(0)
  63.  
  64.  
  65. log.set_module('hp-info')
  66.  
  67. try:
  68.  
  69.     try:
  70.         opts, args = getopt.getopt(sys.argv[1:], 'p:d:hl:b:ig',
  71.             ['printer=', 'device=', 'help', 'help-rest', 'help-man', 
  72.              'help-desc', 'logging=', 'id', 'bus='])
  73.  
  74.     except getopt.GetoptError, e:
  75.         log.error(e.msg)
  76.         usage()
  77.  
  78.     printer_name = None
  79.     device_uri = None
  80.     log_level = logger.DEFAULT_LOG_LEVEL
  81.     bus = device.DEFAULT_PROBE_BUS
  82.     devid_mode = False
  83.  
  84.     if os.getenv("HPLIP_DEBUG"):
  85.         log.set_level('debug')
  86.  
  87.  
  88.     for o, a in opts:
  89.         if o in ('-h', '--help'):
  90.             usage()
  91.  
  92.         elif o == '--help-rest':
  93.             usage('rest')
  94.  
  95.         elif o == '--help-man':
  96.             usage('man')
  97.  
  98.         elif o == '--help-desc':
  99.             print __doc__,
  100.             sys.exit(0)
  101.  
  102.         elif o in ('-p', '--printer'):
  103.             if a.startswith('*'):
  104.                 printer_name = cups.getDefaultPrinter()
  105.                 log.debug(printer_name)
  106.                 
  107.                 if printer_name is not None:
  108.                     log.info("Using CUPS default printer: %s" % printer_name)
  109.                 else:
  110.                     log.error("CUPS default printer is not set.")
  111.                 
  112.             else:
  113.                 printer_name = a
  114.  
  115.         elif o in ('-d', '--device'):
  116.             device_uri = a
  117.  
  118.         elif o in ('-b', '--bus'):
  119.             bus = [x.lower().strip() for x in a.split(',')]
  120.             if not device.validateBusList(bus):
  121.                 usage()
  122.  
  123.         elif o in ('-l', '--logging'):
  124.             log_level = a.lower().strip()
  125.             if not log.set_level(log_level):
  126.                 usage()
  127.  
  128.         elif o == '-g':
  129.             log.set_level('debug')
  130.  
  131.         elif o in ('-i', '--id'):
  132.             devid_mode = True
  133.  
  134.  
  135.     if device_uri and printer_name:
  136.         log.error("You may not specify both a printer (-p) and a device (-d).")
  137.         usage()
  138.  
  139.     if not devid_mode:
  140.         utils.log_title(__title__, __version__)
  141.         
  142.     if os.getuid() == 0:
  143.         log.warn("hp-info should not be run as root.")
  144.  
  145.     if not device_uri and not printer_name:
  146.         try:
  147.             device_uri = device.getInteractiveDeviceURI(bus)
  148.             if device_uri is None:
  149.                 sys.exit(1)
  150.         except Error:
  151.             log.error("Error occured during interactive mode. Exiting.")
  152.             sys.exit(1)
  153.             
  154.     try:
  155.         d = device.Device(device_uri, printer_name)
  156.     except Error:
  157.         log.error("Unexpected error. Exiting.")
  158.         sys.exit(1)
  159.  
  160.     if d.device_uri is None and printer_name:
  161.         log.error("Printer '%s' not found." % printer_name)
  162.         sys.exit(1)
  163.  
  164.     if d.device_uri is None and device_uri:
  165.         log.error("Malformed/invalid device-uri: %s" % device_uri)
  166.         sys.exit(1)
  167.  
  168.     if not devid_mode:
  169.         log.info("")
  170.         log.info(log.bold(d.device_uri))
  171.         log.info("")
  172.  
  173.     user_cfg.last_used.device_uri = d.device_uri
  174.  
  175.     try:
  176.         try:
  177.             d.open()
  178.             d.queryDevice()
  179.         except Error, e:
  180.             log.error("Error opening device (%s)." % e.msg)
  181.             #sys.exit(1)
  182.  
  183.         if not devid_mode:
  184.             formatter = utils.TextFormatter(
  185.                             (
  186.                                 {'width': 28, 'margin' : 2},
  187.                                 {'width': 58, 'margin' : 2},
  188.                             )
  189.                         )
  190.  
  191.         if devid_mode:
  192.             try:
  193.                 log.info(d.dq['deviceid'])
  194.                 sys.exit(0)
  195.             except KeyError:
  196.                 log.error("Device ID not available.")
  197.         else:
  198.             dq_keys = d.dq.keys()
  199.             dq_keys.sort()
  200.  
  201.             log.info(log.bold("Device Parameters (dynamic data):"))
  202.             log.info(log.bold(formatter.compose(("Parameter", "Value(s)"))))
  203.             log.info(formatter.compose(('-'*28, '-'*58)))
  204.  
  205.             for key in dq_keys:
  206.                 log.info(formatter.compose((key, str(d.dq[key]))))
  207.  
  208.             log.info(log.bold("\nModel Parameters (static data):"))
  209.             log.info(log.bold(formatter.compose(("Parameter", "Value(s)"))))
  210.             log.info(formatter.compose(('-'*28, '-'*58)))
  211.  
  212.             mq_keys = d.mq.keys()
  213.             mq_keys.sort()
  214.  
  215.             for key in mq_keys:
  216.                 log.info(formatter.compose((key, str(d.mq[key]))))
  217.  
  218.             if d.dbus_avail:
  219.                 formatter = utils.TextFormatter(
  220.                                 (
  221.                                     {'width': 20, 'margin' : 2}, # date/time
  222.                                     {'width': 5, 'margin' : 2}, # code
  223.                                     {'width': 40, 'margin' : 2}, # desc
  224.                                     {'width': 8, 'margin' : 2}, # user
  225.                                     {'width': 8, 'margin' : 2}, # job id
  226.                                 )
  227.                             )
  228.  
  229.                 log.info(log.bold("\nStatus History (most recent first):"))
  230.                 log.info(log.bold(formatter.compose(("Date/Time", "Code", "Status Description", "User", "Job ID"))))
  231.                 log.info(formatter.compose(('-'*20, '-'*5, '-'*40, '-'*8, '-'*8)))
  232.  
  233.                 hq = d.queryHistory()
  234.  
  235.                 for h in hq:
  236.                     desc = device.queryString(h.event_code)
  237.                     log.info(formatter.compose((time.strftime("%x %H:%M:%S", time.localtime(h.timedate)),  
  238.                         str(h.event_code), desc, h.username, str(h.job_id))))
  239.                 
  240.  
  241.                 log.info("")
  242.     finally:
  243.         d.close()
  244.         
  245. except KeyboardInterrupt:
  246.     log.error("User exit")
  247.     
  248. log.info("")
  249. log.info("Done.")
  250.  
  251.